// miniprogram/pages/user/record-list/record-list.js
import {getStorageSync} from "../../../utils/Storage"
import {getDate} from "../../../utils/Util";
import {deleteRecord, updateRecord} from "../../../service/Record";
import Dialog from "@vant/weapp/dialog/dialog";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        loadListLost: {
            useSlot: false,
            params: {},
            loading: false,
            reset: false,
            list: []
        },
        loadListFound: {
            useSlot: false,
            params: {},
            loading: false,
            reset: false,
            list: []
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setRequestData(options.active)
    },
    ___refreshController: false, //下拉刷新控制器 控制是否可以刷新

    /*
    * 设置请求信息
    * */
    setRequestData: function (active) {
        let openId = getStorageSync("userInfo.openId");
        let params = {personal: true};
        active = parseInt(active) || 0;
        let loadName = active === 0 ? "loadListLost" : "loadListFound";
        params.type = active === 0 ? "lost" : "found";
        openId ? params.openId = openId : null;
        this.setData({
            active: active,
            [loadName + ".params"]: params,
            [loadName + ".loading"]: true
        })
    },

    /*
     * 列表加载组件
     * */
    onLoadList: function (e) {
        const that = this;
        let active = that.data.active;
        let success = e.detail.success;
        let loadName = active === 0 ? "loadListLost" : "loadListFound";
        let list = that.data[loadName].list,
            isReset = that.data[loadName].reset;
        if (success) {
            let newList = e.detail.result.map(function (item) {
                item.createTime = getDate(item.createTime).split(" ")[0];
                item.delLoading = false; //删除加载动画
                item.completeLoading = false; //完成加载动画
                return item
            });
            !isReset ? list.push.apply(list, newList) : list = newList;
            that.setData({
                [loadName + ".list"]: list,
                [loadName + ".reset"]: false,
                [loadName + ".useSlot"]: list.length === 0
            })
            that.___refreshController = true;

        }
    },

    /*
    * tabs切换
    * */
    onTabsChange: function (e) {
        this.setRequestData(e.detail.index)
    },

    /*
    * 点击栏目跳转
    * */
    bindNavigatorTo: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/user/record-detail/record-detail?id=" + id
        })
    },

    /*
    * 点击按钮
    * */
    bindClick: async function (e) {
        let name = e.currentTarget.dataset.name;
        let todo = e.currentTarget.dataset.todo;
        let index = e.currentTarget.dataset.index;
        let list = this.data[name].list;
        let id = list[index]._id;
        switch (todo) {
            case "edit":
                wx.navigateTo({
                    url: "/packageUser/pages/edit-record/edit-record?id=" + id
                })
                break
            case "delete":
                try {
                    await Dialog.confirm({
                        title: "提示",
                        message: "是否删除该条信息",
                    })
                    this.setData({[name + ".list[" + index + "].delLoading"]: true})
                    await deleteRecord(id);
                    list.splice(index, 1);
                    this.setData({
                        [name + ".list"]: list,
                        [name + ".useSlot"]: list.length === 0
                    })
                } catch (e) {
                    e.status ? Toast.fail("删除失败") : null;
                    this.setData({[name + ".list[" + index + "].delLoading"]: false})
                }
                break
            case "complete":
                try {
                    this.setData({[name + ".list[" + index + "].completeLoading"]: true})
                    await updateRecord(id, {complete: 1});
                    this.setData({[name + ".list[" + index + "].complete"]: 1})
                } catch (e) {
                    Toast.fail("更新状态失败");
                    this.setData({[name + ".list[" + index + "].completeLoading"]: false})
                }
                break
            case "detail":
                wx.navigateTo({
                    url: "/pages/user/record-detail/record-detail?id=" + id
                })
                break
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        let active = this.data.active;
        let loadName = active === 0 ? "loadListLost" : "loadListFound"
        if (this.___refreshController) {
            this.___refreshController = false;
            this.setData({[loadName + ".reset"]: true});
            setTimeout(function () {
                wx.stopPullDownRefresh();
            }, 1500)
        } else {
            wx.stopPullDownRefresh();
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let active = this.data.active;
        let loadName = active === 0 ? "loadListLost" : "loadListFound"
        this.setData({[loadName + ".loading"]: true})
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    onShareTimeline: function () {}
})